/**
 * 
 */
package org.tq.test.thread;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

/**
 * @title 		
 * @description	
 * @usage		
 * @company		上海微汇信息技术有限公司
 * @author		TQSUMMER
 * @create		2013-3-6 下午4:40:14
 */
/**
 * @author TQSUMMER
 */
public class TimeoutThreadPool {
	public static void main(String[] args) {
		ThreadPoolExecutor executor = new ThreadPoolExecutor(10, 10, 1, TimeUnit.SECONDS,
				new LinkedBlockingDeque<Runnable>(20));
		Object result = false;
		Future<Object> future = executor.submit(new Callable<Object>() {

			public Object call() throws Exception {
				System.out.println("1");
				Thread.sleep(5000);
				System.out.println("2");
				return "3";
			}

		});// 将任务提交到线程池中
		try {
			result = future.get(2000, TimeUnit.MILLISECONDS);// 设定在200毫秒的时间内完成
			System.out.println(result);
		} catch (InterruptedException e) {

			e.printStackTrace();
		} catch (ExecutionException e) {

			e.printStackTrace();
		} catch (TimeoutException e) {
			System.out.println("Time is out");// 抛出超时异[size=xx-small][/size]常时打印
		}
	}
}
